#!/bin/bash
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/alias.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/conda_run.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/conda_and_pip_settings.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/print_sys_info.sh"

APP_TARGET_DIR_NAME="n8n-workflows"

export CURRENT_APP_PATH="$LMD_APPS_DIR/$APP_TARGET_DIR_NAME"
echo "App Dir: ${CURRENT_APP_PATH}"

cd "$CURRENT_APP_PATH"

if [ -z "$HF_ENDPOINT" ] && [ -n "$HF_MIRROR" ]; then
    export HF_ENDPOINT="$HF_MIRROR"
fi

CONDA_PREFIX="./venv"

APP_FILE="run.py"

PORT_PARAM=""
if [ -n "$GRADIO_SERVER_PORT" ]; then
  PORT_PARAM="--port $GRADIO_SERVER_PORT"
fi

# replace js lib url
OLD_LIB_URL="https://cdn.jsdelivr.net/npm/mermaid@10.6.1/dist/mermaid.min.js"
NEW_LIB_URL="https://daiyl.com/pkgs/web-static/mermaid.min.js"
if [ "$LMD_LOCALE" = "zhHans" ]; then
  NEW_LIB_URL="https://seemts.com/pkgs/web-static/mermaid.min.js"
fi
conda_run_cmd python ${LMD_BASE_INSTALL_SCRIPT_DIR}/global/replace_str.py "$CURRENT_APP_PATH/static/index.html" "$OLD_LIB_URL" "$NEW_LIB_URL"
conda_run_cmd python ${LMD_BASE_INSTALL_SCRIPT_DIR}/global/replace_str.py "$CURRENT_APP_PATH/static/index-nodejs.html" "$OLD_LIB_URL" "$NEW_LIB_URL"

export CI=""
conda run -p $CONDA_PREFIX --no-capture-output python $APP_FILE $PORT_PARAM --reindex
